Previous Book Contents Book Index Next

Inside Macintosh: AppleScript Language Guide / Part 2 - AppleScript Language Reference
Chapter 5 - Objects and References


Using the Filter Reference Form

When specifying one or more objects contained in an application object, you can use the Filter reference form to include an optional filter. A filter restricts the objects you specify to those that match one or more conditions.

For example, compare this reference without a filter

every word of paragraph 5
to the same reference with a filter:

every word of paragraph 5 where character 1 = "M"
The first reference specifies all the words in the fifth paragraph. The second reference, which includes the filter where character 1 = "M", specifies all the words in the same container whose first character is "M". Words that do not pass this test are filtered out.

In effect, a filter reduces the number of objects in the container. Instead of specifying every word of the fifth paragraph, the reference

every word of paragraph 5 whose first character = "M"
specifies every word of a smaller container, the words of the fifth paragraph whose first characters are "M". Similarly,

words 1 thru 5 of paragraph 5 whose first character = "M"
specifies the first five words of the same smaller container.

To determine the objects in the smaller container, the application applies the filter to all of the objects of the specified class in the specified container--in this case, the words in the fifth paragraph. The application uses the filter to test each object in turn, starting with the first.

Within a filter, the predefined variable it refers to the object currently being tested. For example, in the reference

second paragraph of document "Product Intro"  ÿ   where it contains "dynamo"
the word it refers to each paragraph in the document Product Intro. The
filter, contains "dynamo", is applied to each paragraph in the document, resulting in a smaller container whose paragraphs all contain the string "dynamo". The reference specifies the second paragraph of that smaller container.

A Filter reference includes one or more tests. Each test is a Boolean expression that compares a property or element of each object being tested, or the objects themselves, with another object or value. Table 5-2 shows some Filter references, the Boolean expressions they contain, and what is being tested in each reference.
Boolean expressions and tests in Filter references
Filter referenceBoolean expressionWhat is
being tested
words whose length > 10length > 10The length property of
each word
words whose first character = "M"first character = "M"The first
character of
each word
words where it contains "el"it contains "el"The words themselves

Note
A test can be any Boolean expression (such as words where 1 < 2), but only those that actually test objects
or their contents are useful for filtering objects.
To include more than one test in a filter, link the tests with Boolean operators,
as in

words whose length > 10 and tenth character = "M"
The Boolean operator And indicates that each word must pass both tests to be included in the smaller container. Another example is

words where it contains "M" or it contains "G"
The Boolean operator Or indicates that the words can pass either test to be included in the smaller container.

Because each test is a Boolean expression, it can also include the Boolean operator Not. For example, the reference

words whose length > 10 and not it contains "M"
refers to only those words containing more than ten characters and not containing the letter "M." The expression it contains "M" is a valid Boolean expression, and applying the Boolean Not operator to it, as in

not (it contains "M")
inverts the value of the expression, so that a true value becomes false, and a false value becomes true.

A more elegant way to apply the Boolean Not operator to the expression it contains "M" is

it doesn't contain "M"
The expression it doesn't contain "M" is a synonym for the expression not (it contains "M"). AppleScript supports synonyms for many of its operators. Using a synonym doesn't change the meaning of an expression, but it can make the expression easier to read. Operators and synonyms are listed in Chapter 6, "Expressions."


Previous Book Contents Book Index Next

© Apple Computer, Inc.
13 JUL 1996